package edu.wayne.cs.csc4110.hw6.UnitTests;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import edu.wayne.cs.csc4110.hw6.framework.GameAuthentication;
import edu.wayne.cs.csc4110.hw6.framework.Util.UserFileFilter;
import edu.wayne.cs.csc4110.hw6.framework.state.UserStateImpl;
import edu.wayne.cs.csc4110.hw6.interfaces.state.UserState;
import edu.wayne.cs.csc4110.hw6.plugins.example.meteor.MeteorPlugin;
import edu.wayne.cs.csc4110.hw6.plugins.example.meteor.MeteorUser;

public class BasicTest {
	private MeteorUser u;
	private UserState userState;
	private GameAuthentication auth;
	private MeteorPlugin plugin;

	@Before
	public void setUp() throws IOException {
		u = new MeteorUser("James");
		plugin = new MeteorPlugin();
		userState = new UserStateImpl(u.getUsername(),plugin.getUserExtension() );
		userState.storeState(null, u);
		auth = new GameAuthentication(plugin);
	}

	@After
	public void tearDown() {
		File dir = new File(GameAuthentication.DIRECTORY);
		FilenameFilter filter = new UserFileFilter();
		for (File f : dir.listFiles(filter)) {
			if (!f.delete()) {
				System.err.println(f.getName() + " was not deleted");
			}

		}
	}

	@Test
	public void defaultNullStateStore() {
		try {
			assertEquals(true, userState.storeState(null, u));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	@Test
	public void defaultNullStateCreation() throws IOException,
			ClassNotFoundException {
		assertEquals(u, userState.createState(null));

		u.incrementScore(10);
		userState.storeState(null, u);
		MeteorUser testUser = (MeteorUser) userState.createState(null);

		assertEquals(u.getScore(), testUser.getScore());
	}

	@Test
	public void authentication() throws IOException {
		assertEquals(u, auth.login("James"));
		assertEquals(new MeteorUser("unkownuser"), auth.login("unkownuser"));
	}

}
